﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.Data.Services.Client;

namespace FeedReader.Shared.Data
{
    public class UserSubscrptionsDataSource
    {
        private UserSubscrptionsDataServiceContext _ServiceContext = null;

        public UserSubscrptionsDataSource(CloudStorageAccount storageAccount)
        {
            //var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
            _ServiceContext = new UserSubscrptionsDataServiceContext(storageAccount.TableEndpoint.ToString(), storageAccount.Credentials);

            // Create the tables
            // In this case, just a single table.  
            storageAccount.CreateCloudTableClient().CreateTableIfNotExist(UserSubscrptionsDataServiceContext.UserSubscrptionsTableName);
        }

        public IEnumerable<UserSubscrptionsDataModel> Select()
        {
            var results = from c in _ServiceContext.UserSubscrptionsTable
                          orderby c.DateTime
                          select c;

            var query = results.AsTableServiceQuery<UserSubscrptionsDataModel>();
            var queryResults = query.Execute();

            return queryResults;
        }
        //public IEnumerable<UserSubscrptionsDataModel> Select(string hash)
        //{
        //    var results = from c in Select()
        //                  where c.Hash==hash
        //                  select c;

        //    return results;
        //}

        //public IEnumerable<UserSubscrptionsDataModel> SelectBlog(string id)
        //{
        //    var results = from c in Select()
        //                  where c.BlogID==id
        //                  select c;

        //    return results;
        //}

        //public IEnumerable<UserSubscrptionsDataModel> WhereDate(DateTime date)
        //{
        //    var results = from c in Select()
        //                  where c.DateTime<date
        //                  select c;

        //    return results;
        //}
        //public void Delete(UserSubscrptionsDataModel itemToDelete)
        //{
        //    _ServiceContext.AttachTo(UserSubscrptionsDataServiceContext.UserSubscrptionsTableName, itemToDelete, "*");
        //    _ServiceContext.DeleteObject(itemToDelete);
        //    _ServiceContext.SaveChanges();
        //}
        //public void DeleteRange(IEnumerable<UserSubscrptionsDataModel> itemsToDelete)
        //{
        //    foreach (UserSubscrptionsDataModel item in itemsToDelete)
        //    {
        //        Delete(item);
        //    }
        //}

        public void Insert(UserSubscrptionsDataModel newItem)
        {
            _ServiceContext.AddObject(UserSubscrptionsDataServiceContext.UserSubscrptionsTableName, newItem);
            _ServiceContext.SaveChanges();
        }

        public IEnumerable<UserSubscrptionsDataModel> Select(string id)
        {
            var results = from c in Select()
                          where c.BlogID == id
                          select c;

            return results;
        }
    }
}
